package com.lm;

import mapgenerator.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 *
 * @author Sajarwo Anggai
 */
public class WorkSpace implements ActionListener, MouseListener, MouseMotionListener {

    private Main comp;
    private int tileX, tileY, mapX, mapY;

    public WorkSpace(Main comp) {
        this.comp = comp;
        comp.ToolbarP.addActionListener(this);
        comp.ToolbarS.addActionListener(this);
    }

    public static int pixelsToTiles(int pixels, int tileSize) {
        return (int) Math.floor((float) pixels / tileSize);
    }

    public static int tilesToPixels(int numTiles, int tileSize) {
        return numTiles * tileSize;
    }
    private Object currentToolbarButton;

    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        if (ob == comp.ToolbarP) {
            currentToolbarButton = comp.ToolbarP;
            System.out.println("yup= " + comp.ToolbarP.isSelected());
        }
    }

    public void drawTile() {
        try {
            if (comp.ToolbarP.isSelected()) {
                int row = comp.lmControlPanel.getTable().getSelectedRow();
                if (row != -1) {
                    Layer layer = (Layer) comp.layerContainer.getLayers().get(row);
                    if (layer.isShow()) {
                        System.out.println("Tile(x,y)=" + tileX + "," + tileY);
                        layer.getTileContaner()[tileX][tileY] = comp.ipControlPanel.getCurrentTile();
                        comp.lmControlPanel.gridWorkspace.setDraw(true);
                        comp.lmControlPanel.gridWorkspace.drawLayer(tileX, tileY);
                    }
                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        setViewTile(e.getX(), e.getY());

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
        drawTile();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
    }

    public void setViewTile(int x, int y) {
        comp.InfoMapX.setText("x:" + x);
        comp.InfoMapY.setText("y:" + y);
        mapX = x;
        mapY = y;

        x = Math.max(pixelsToTiles(x, comp.project.getTileWidth()),0);
        y = Math.max(pixelsToTiles(y, comp.project.getTileHeight()),0);
        comp.InfoTileX.setText("x:" + x);
        comp.InfoTileY.setText("y:" + y);
        tileX = x;
        tileY = y;
    }

    public int getMapX() {
        return mapX;
    }

    public int getMapY() {
        return mapY;
    }

    public int getTileX() {
        return tileX;
    }

    public int getTileY() {
        return tileY;
    }

    
}
